package Tast;

public class Test1 {
    class Solution {
        public String longestCommonPrefix(String[] strs) {
            //解法一：两两比较
            String ret = strs[0];
            for(int i =  1 ; i < strs.length;i++){
                ret = findCommon(strs[i],ret);


            }
            return ret;
        }
        public String findCommon(String s1,String s2){
            int i = 0;
            while(i < Math.min(s1.length() , s2.length()) && s1.charAt(i) == s2.charAt(i))

                i++;
            return s1.substring(0 ,i);
        }
    }

    class Solution1 {
        public String longestCommonPrefix(String[] strs) {
            //解法二:统一比较
            for(int i = 0; i < strs[0].length() ;i++){
                char tmp = strs[0].charAt(i);
                for(int j = 1 ; j < strs.length;j++){
                    if(i == strs[j].length() || strs[j].charAt(i) != tmp)
                        return strs[0].substring(0 , i);
                }
            }
            return strs[0];
        }
    }
}
